<?php

/**
 * Description of Log
 *
 * @author superx
 * @package Core
 */
class Core_Log {

    const LEVEL_NONE = 0x00;
    const LEVEL_ALL = 0xFF;
    const LEVEL_DEBUG = 0x01;

    const LEVEL_PROD_DEBUG = 0x10;
    const LEVEL_PROD = 0x80;

    /**
     *
     * @param mixed $var
     * @param string $name
     * @param string $line_delimiter
     */
    public static function log($var, $name = 'all', $line_delimiter = "\n") {
        $file = Core_Config::get('Core.logDir', PROJECT_PATH . 'Log/' . ENV) . '/' . $name . '.log';
        if (!is_string($var)) {
            $var = var_export($var, true);
        }
        $var = '[' . date('Y-m-d H:i:s') . '] ' . $var;
        error_log($var . $line_delimiter, 3, $file);
    }

    public static function logErr($var, $name = 'ERR') {
        if (!($var instanceof Exception)) {
            if (is_string($var)) {
                $var = new Exception($var);
            } else {
                $var = new Exception(var_export($var, true));
            }
        }
        self::log($var->getMessage() . "\n" . $var->getTraceAsString(), $name);
    }

    /**
     * triggerred under debug level
     * @param mixed $var1 , $var2, $var3 ...
     */
    public static function debug() {
        if (Core_Config::get('Core.logLevel', self::LEVEL_NONE) & self::LEVEL_DEBUG) {
            $args = func_get_args();
            $args_ct = func_num_args();
            $prefix = '';
            $filename = 'debug';
            if ($args_ct > 1) {
                if (is_string($args[$args_ct - 1]) && false !== strpos($args[$args_ct - 1], 'debug')){
                    $filename = $args[$args_ct - 1];
                    array_pop($args);
                }
                if (is_string($args[0])){
                    $prefix = "[$args[0]] ";
                    array_shift($args);
                }
            }
            foreach ($args as $var) {
                if (!is_string($var)) {
                    $var = var_export($var, true);
//                    $var = str_replace("\n", '', $var);
                }
                self::log($prefix . $var, $filename, "\n");
            }
            self::log('---------------------------', $filename, "\n");
        }
    }

}